package com.lp.biz.operations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lp.biz.content.mapper.BizCoreContentMapper;
import com.lp.biz.content.model.entity.BizCoreContent;
import com.lp.biz.operations.model.vo.CategoryAdd;
import com.lp.biz.operations.model.vo.CategoryList;
import com.lp.biz.operations.service.CategoryService;
import com.lp.common.entity.PageParams;
import com.lp.common.exception.BaseException;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.lp.common.constant.CategoryConstant.*;
import static com.lp.common.constant.CommonConstant.NEGATE;
import static com.lp.common.constant.CommonConstant.SURE;
import static com.lp.common.utils.Tools.copyPage;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-07-18 21:02:27
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private BizCoreContentMapper coreContentMapper;

    @Override
    public CommonResult getContentList(PageParams query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        List<BizCoreContent> contents = coreContentMapper.selectList(new QueryWrapper<BizCoreContent>().lambda()
                .eq(BizCoreContent::getIsRecommend, SURE)
                .eq(BizCoreContent::getIsDeleted, NEGATE)
                .orderByDesc(BizCoreContent::getCreateAt));
        Page<CategoryList> categoryLists = copyPage(new Page<>(), contents);
        for (BizCoreContent content : contents) {
            categoryLists.add(CategoryList.builder(content));
        }
        return CommonResult.restPage(categoryLists);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addRecommend(CategoryAdd params) {
        BizCoreContent content = coreContentMapper.selectById(params.getContentId());
        if (content == null || content.getIsDeleted().equals(SURE) || content.getIsDisplay().equals(NEGATE)
                || content.getIsRecommend().equals(SURE)) {
            throw new BaseException("内容输入错误");
        }
        String code = content.getBizCategoryCode();
        if (!code.equals(CODE_READING) && !code.equals(CODE_STORY) && !code.equals(CODE_TRAINING)) {
            throw new BaseException("内容输入错误");
        }
        if (params.getEndAt().isBefore(params.getStartAt())) {
            throw new BaseException("展示时间输入错误");
        }
        content.setIsRecommend(SURE);
        content.setRecommendStartAt(params.getStartAt());
        content.setRecommendEndAt(params.getEndAt());
        coreContentMapper.updateById(content);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteCategoryRecommend(Integer contentId) {
        BizCoreContent content = coreContentMapper.selectById(contentId);
        if (content == null || content.getIsDeleted().equals(SURE) || content.getIsDisplay().equals(NEGATE)
                || content.getIsRecommend().equals(NEGATE)) {
            throw new BaseException("内容输入错误");
        }
        coreContentMapper.update(null, new UpdateWrapper<BizCoreContent>().lambda()
                .eq(BizCoreContent::getId, content.getId())
                .set(BizCoreContent::getIsRecommend, NEGATE)
                .set(BizCoreContent::getRecommendStartAt, null)
                .set(BizCoreContent::getRecommendEndAt, null));
    }
}
